package com.yjzx.binlog.config;

import com.yjzx.util.binlog.core.config.BinLogBean;
import com.yjzx.util.binlog.core.executor.BinLogExecutor;
import com.yjzx.util.binlog.core.handle.BinlogHandle;
import com.yjzx.util.binlog.core.listener.MysqlBinLogListener;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;

/**
 * @author yjzx
 * @date 2024/8/15
 * @description 初始化注册binlog监听器
 */
@Slf4j
@Configuration
@RequiredArgsConstructor
public class MyCommandLineRunner implements CommandLineRunner {
    private final BinLogProperties binlogProperties;

    private final List<BinLogExecutor> binlogExecutors;

    @Bean
    public BinLogBean BinLogBean(BinLogProperties properties) {
        return properties.toBinLogBean();
    }

    @Bean
    public MysqlBinLogListener mysqlBinLogListener(BinLogProperties properties) {
        return new MysqlBinLogListener(properties.toBinLogBean());
    }

    @Override
    public void run(String... args) {
        new BinlogHandle(BinLogBean(binlogProperties), mysqlBinLogListener(binlogProperties), binlogExecutors).start();
    }


}
